package com.uniinnovate.lanjingling.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.uniinnovate.lanjingling.entity.JobEntity;
import com.uniinnovate.lanjingling.pojo.vo.JobApplicationVO;
import com.uniinnovate.lanjingling.service.JobService;
import com.uniinnovate.lanjingling.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uniinnovate.lanjingling.utils.PageUtils;
import com.uniinnovate.lanjingling.utils.Query;

import com.uniinnovate.lanjingling.dao.JobApplicationDao;
import com.uniinnovate.lanjingling.entity.JobApplicationEntity;
import com.uniinnovate.lanjingling.service.JobApplicationService;


@Service("jobApplicationService")
public class JobApplicationServiceImpl extends ServiceImpl<JobApplicationDao, JobApplicationEntity> implements JobApplicationService {

    @Autowired
    private JobService jobService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<JobApplicationEntity> page = this.page(
                new Query<JobApplicationEntity>().getPage(params),
                new QueryWrapper<JobApplicationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R submitApply(JobApplicationVO jobApplicationVO) {
        JobApplicationEntity jobApplication = new JobApplicationEntity();
        BeanUtils.copyProperties(jobApplicationVO, jobApplication);
        boolean save = save(jobApplication);
        if(save){
            return R.ok("提交申请成功");
        }else {
            return R.error("提交申请失败");
        }
    }

    @Override
    public R listApply(Long id) {
        R r = jobService.listJob(id);
        ArrayList<JobEntity> data = (ArrayList<JobEntity>) r.get("data");
        Map<String, Object> map = new HashMap<>();
        List<JobApplicationEntity> result = new ArrayList<>();
        for(JobEntity jobEntity : data){
            Long jobId = jobEntity.getId();
            QueryWrapper<JobApplicationEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("job_id",jobId);
            List<JobApplicationEntity> list = list(queryWrapper);
            result = Stream.of(result, list).flatMap(Collection::stream).collect(Collectors.toList());
        }
        map.put("data",result);
        return R.ok(map);
    }

    @Override
    public R auditApply(Long applyId) {
        
        return null;
    }


}